package com.ryhx.util;

import java.io.UnsupportedEncodingException;

/**
 * 将配置文件中的所有配置项 都初始化到工具类中  
 * 方便其他类调用
 * @author yaojinshan
 * @since 2017-08-02 10:14
 */
public class ConfigUtil {
	public static PropertiesUtil util = new PropertiesUtil("config.properties");//资源文件读取工具类实例对象
	
	public static String simpleModelName = "";//model名称
	public static String firstCharLowerSimpleModelName = "";//model 首字母小写名称
	public static String simpleModelNameLower =  "";//model转成小写 名称
	public static String excetpionName = "";//异常的全路径名称
	public static String simpleExceptionName = "";//异常的名称
	public static String modelPackagePath = "";//model 路径
	public static String daoPackagePath = "";//dao 路径
	public static String servicePackagePath = "";//service 路径
	public static String controllerPackagePath = "";//controller路径
	public static String database_table = "";//数据库表名

	public static String driverClassName =  "";//数据库驱动
	public static String url =  "";//数据库连接串
	public static String username = "";//数据库用户名
	public static String password =  "";//数据库密码

	public static String remove_table_prefix = "";//生成的model需要去掉的前缀 

	public static String entityName = "";//实体中文名
	
	static{
		initProperties();
		System.out.println("数据库表名:" + database_table + ",移除的前缀是：" + remove_table_prefix);
		System.out.println("model 名称:" + simpleModelName);
		System.out.println("model 首字母小写名称:" + firstCharLowerSimpleModelName);
		System.out.println("model 转成小写 名称:" + simpleModelNameLower);
		System.out.println("model 路径:" + modelPackagePath);
		System.out.println("dao 路径:" + daoPackagePath);
		System.out.println("service 路径：" + servicePackagePath);
		System.out.println("controller 路径：" + controllerPackagePath);
		System.out.println("entityName 的值 ：" + entityName);
	}
	/**
	 * 初始化资源文件的属性到工具类
	 */
	private static void initProperties() {
		database_table = util.getProperty("database_table");
		if(database_table == null || "".equals(database_table)){
			try {
				throw new Exception("请填写数据库表名");
			} catch (Exception e) {
				System.out.println(e);
				System.exit(0);
			}
		}
		remove_table_prefix = util.getProperty("remove_table_prefix");
		String tempModelName = database_table.replace(remove_table_prefix, "");//移除掉表前缀
		simpleModelName = getSimpleModelName(tempModelName);
		simpleModelNameLower = simpleModelName.toLowerCase();
		firstCharLowerSimpleModelName = new StringBuilder().append(simpleModelName.substring(0, 1).toLowerCase()).append(simpleModelName.substring(1)).toString();
		excetpionName = util.getProperty("exception");
		simpleExceptionName = excetpionName.substring(excetpionName.lastIndexOf(".") + 1, excetpionName.length());
		modelPackagePath = util.getProperty("model.package.path");
		daoPackagePath = util.getProperty("dao.package.path");
		servicePackagePath = util.getProperty("service.package.path");
		controllerPackagePath = util.getProperty("controller.package.path");
		driverClassName = util.getProperty("jdbc.driverClassName");
		url = util.getProperty("jdbc.url");
		username = util.getProperty("jdbc.username");
		password = util.getProperty("jdbc.password");
		try {
			if(util.getProperty("entityName") != null){
				entityName = new String(util.getProperty("entityName").getBytes("iso-8859-1"),"utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 获取 model简单名字 即 bean 实体名称 
	 * @param tempModelName
	 */
	private static String getSimpleModelName(String tempModelName) {
		String[] tempArray = tempModelName.split("_");
		StringBuilder sb = new StringBuilder();
		for(String str : tempArray){
			sb.append(str.substring(0, 1).toUpperCase()).append(str.substring(1));
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		
	}
}
